#include "TcpServer.hpp"
#include <memory>
using namespace Tcp_server;
//./server + port
Response calculate(const Request &rq)
{
    Response rp(0,0);
    switch (rq.op_)
    {
    case '+':
        rp.result_ = rq.x_ + rq.y_;
        break;
    case '-':
        rp.result_ = rq.x_ - rq.y_;
        break;
    case '*':
        rp.result_ = rq.x_ * rq.y_;
        break;
    case '/':
        if (rq.y_ == 0)
        {
            rp.exitcode_ = 1;
            break;
        }
        rp.result_ = rq.x_ / rq.y_;
        break;
    case '%':
        if (rq.y_ == 0)
        {
            rp.exitcode_ = 2;
            break;
        }
        rp.result_ = rq.x_ + rq.y_;
        break;
    default:
        rp.exitcode_ = 3;
        break;
    }
    return rp;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);
    
    // int fd = open("test.txt",O_RDONLY);
    // char buffer[10] = {0};
    // read(fd,buffer,3);
    // cout<<buffer<<endl;
    // read(fd,buffer,3);
    // cout<<buffer<<endl;
    std::unique_ptr<Tcpserver> tcp(new Tcpserver(calculate, port));
    tcp->Init();
    tcp->start();
    return 0;
}